

getTime = function () {
    var now = new Date();

    var year = now.getFullYear();       //年
    var month = now.getMonth() + 1;     //月
    var day = now.getDate();            //日

    var hh = now.getHours();            //时
    var mm = now.getMinutes();          //分

    var clock = year + "-";

    if(month < 10)
        clock += "0";

    clock += month + "-";

    if(day < 10)
        clock += "0";

    clock += day + " ";

    if(hh < 10)
        clock += "0";

    clock += hh + ":";
    if (mm < 10) clock += '0';
    clock += mm;
    return clock;
}

//时间控件的设置
$(".form_datetime").datetimepicker({
    format: "dd MM yyyy - hh:ii",
    autoclose: true,
    todayBtn: true,
    startDate: getTime(),
    minuteStep: 10
});



//获取添加课程按钮
var oAddCourse = document.getElementById("addCourse");

//添加课程
oAddCourse.onclick = function () {
    $("#myModal").hide();
    $("#myForm").ajaxSubmit(function(message) {
        // 对于表单提交成功后处理，message为返回内容
        CourseList();
    });
}

var oShowModal = document.getElementById("showModal");

oShowModal.onclick = function () {
    $("#changeCourseInfo").css("display","none");
    $("#addCourse").css("display","block");
    /*$("#myForm").attr("action","createCourse");*/
    document.getElementById("myForm").action = "createCourse";

    $("#courseName").val("");
    $("#startTime").val("");
    $("#endTime").val("");
}

//获取课程列表
CourseList = function () {
    $("#showModal").css("display","block");//显示创建课程的button
    $("#showAssignmentModal").css("display","none");//隐藏创建作业的button
    $("#showActivityModal").css("display","none");//隐藏创建活动的button

    $('#CourseTable').bootstrapTable('destroy');
    $('#CourseTable').bootstrapTable({
        //样式调整
        formatter: true,
        /*detailView: true,*/
        showFooter: false,
        //后台获取数据
        url: 'getAllCourse',
        //id
        idField: 'courseId',
        columns: [{
            field: 'courseId',
            title: 'courseId',
            visible: false
        },{
            field: 'courseName',
            title: '课程名',
        }, {
            field: 'userId',
            title: 'userId',
            visible: false
        },{
            field: 'startTime',
            title: 'startTime',
        },{
            field: 'endTime',
            title: 'endTime',
        },{
            field: 'operation',
            title: '操作',
            formatter: function (value, row, index) {
                var jsonRow = JSON.stringify(row);
                return "<button class='btn btn-info btn-sm' id='jumpAssByCourseId'"
                    + "onclick='AssignmentList(" + row.courseId + ")'>进入</button>" +
                    "&nbsp;&nbsp;<button class='btn btn-info btn-sm' " +
                    "data-toggle='modal' data-target='#myModal' " +
                    "onclick='showModifyCourseInfoModal(" + jsonRow + ")'>编辑</button>" +
                    "&nbsp;&nbsp;<button class='btn btn-info btn-sm' data-toggle='modal'" +
                    "data-target='#myDeleteModal' onclick=" +
                    "showDeleteModal(" + row.courseId + ",'course','course')>删除</button>";
            }
        }]
    });
}

showDeleteModal = function (elementId,elementParam,deleteType) {
 if(deleteType=="course"){
     $("#confirmDelete").on("click",{elementId: elementId},deleteCourse);
 }
 if(deleteType=="assignment"){
     $("#confirmDelete").off("click");
     $("#confirmDelete").on("click",{elementId: elementId,elementParam:elementParam},deleteAssignment);
 }
 if(deleteType=="activity"){
     $("#confirmDelete").off("click");
     $("#confirmDelete").on("click",{elementId: elementId,elementParam:elementParam},deleteActivity);
 }
}

$("#confirmDelete").click(function () {

})

showModifyCourseInfoModal = function (jsonRow) {
    document.getElementById("myForm").action = "modifyCourse";
/*    $("#myForm").attr("action","modifyCourse");*/
    $("#changeCourseInfo").css("display","block");
    $("#addCourse").css("display","none");
    $("#courseId").val(jsonRow.courseId);
    $("#courseName").val(jsonRow.courseName);
    $("#startTime").val(jsonRow.startTime);
    $("#endTime").val(jsonRow.endTime);
}

var oChangeCourseInfo = document.getElementById("changeCourseInfo");

oChangeCourseInfo.onclick = function () {
    console.log(document.getElementById("myForm").action);
    console.log("修改课程信息");
    $("#myModal").hide();
    $("#myForm").ajaxSubmit(function(message) {
        // 对于表单提交成功后处理，message为返回内容
        CourseList();
        $("#courseName").val("");
        $("#startTime").val("");
        $("#endTime").val("");
    });
}

deleteCourse = function (event) {
    var courseId = event.data.elementId;
    $.ajax({
        url: 'deleteCourse',
        type: 'post',
        data: {
            courseId:courseId
        },
        success: function () {
            CourseList();
        }
    })
}

//Assignment的相关函数
//获取assignment集合table
function AssignmentList(courseId){

    $("#showModal").css("display","none");//隐藏创建课程的button
    $("#showAssignmentModal").css("display","block");//显示创建作业的button
    $("#showActivityModal").css("display","none");//隐藏创建活动的button

    $("#courseIdConnAssignment").val(courseId);//将courseId存入一个hidden的input框

    $('#CourseTable').bootstrapTable('destroy');
    $('#CourseTable').bootstrapTable({
        //样式调整
        formatter: true,
        //后台获取数据
        url: 'getAllAssignment',
        method: "post",
        contentType:"application/x-www-form-urlencoded; charset=UTF-8",
        queryParams: getParams(courseId),
        dataType: "json",
        //idc
        idField: 'assignmentId',
        columns: [{
            field: 'assignmentd',
            title: 'assignmentId',
            visible: false
        },{
            field: 'courseId',
            title: 'courseId',
            visible: false
        },{
            field: 'assignmentName',
            title: '课程名',
        }, {
            field: 'description',
            title: '描述',
        },{
            field: 'startTime',
            title: 'startTime',
        },{
            field: 'endTime',
            title: 'endTime',
        },{
            field: 'operation',
            title: '操作',
            formatter: function (value, row, index) {
                var jsonRow = JSON.stringify(row);
                return "<button class='btn btn-info btn-sm' " +
                    "id='jumpActByAssignmentId' onclick=" +
                    "'ActivityList(" + row.assignmentId + "," + courseId + ")'>进入</button>" +
                    "&nbsp;&nbsp;<button class='btn btn-info btn-sm' data-toggle='modal'"+
                    "data-target='#myAssignmentModal' onclick='" +
                    "showModifyAssignmentInfoModal(" + jsonRow + ")'>编辑</button>&nbsp;" +
                    "&nbsp;" + "<button class='btn btn-info btn-sm' data-toggle='modal'" +
                    " data-target='#myDeleteModal' onclick=" +
                    "showDeleteModal(" + row.assignmentId + "," + courseId + ",'assignment')>删除</button>";
            }
        }]
    });
}
//传递参数
getParams = function (courseId) {
    var param = {
        courseId: courseId
    };
    return param;
};

//更新Assignment信息
showModifyAssignmentInfoModal = function (jsonRow) {

    //获取assignment的信息，并显示在输入框
    $("#changeAssignmentInfo").css("display","block");
    $("#addAssignment").css("display","none");
    $("#assignmentId").val(jsonRow.assignmentId);
    $("#assignmentName").val(jsonRow.assignmentName);
    $("#assignmentDescription").val(jsonRow.description);
    $("#assignmentStartTime").val(jsonRow.startTime);
    $("#assignmentEndTime").val(jsonRow.endTime);
};

var oChangeAssignmentInfo = document.getElementById("changeAssignmentInfo");

oChangeAssignmentInfo.onclick = function () {

    $("#myAssignmentModal").hide();
    $.ajax({
        url: 'modifyAssignment',
        type: 'post',
        data: {
            assignmentId: $("#assignmentId").val(),
            assignmentName: $("#assignmentName").val(),
            description: $("#assignmentDescription").val(),
            startTime: $("#assignmentStartTime").val(),
            endTime: $("#assignmentEndTime").val()
        },
        success: function () {
            AssignmentList($("#courseIdConnAssignment").val());
        }
    });
}

//删除Assignment
deleteAssignment = function (event) {
    var assignmentId = event.data.elementId;
    var courseId = event.data.elementParam;
    $.ajax({
        url: 'deleteAssignment',
        type: 'post',
        data: {
            assignmentId:assignmentId
        },
        success: function () {
            AssignmentList(courseId);
        }
    })
}

//获取添加作业按钮
var oAddAssignment = document.getElementById("addAssignment");

//添加作业
oAddAssignment.onclick = function () {
    console.log("开始时间："+$("#assignmentStartTime").val());
    var courseId = $("#courseIdConnAssignment").val();
    $("#myAssignmentModal").hide();
    $.ajax({
        url: 'createAssignment',
        type: 'post',
        data: {
            courseId:courseId,
            assignmentName: $("#assignmentName").val(),
            description: $("#assignmentDescription").val(),
            startTime:$("#assignmentStartTime").val(),
            endTime:$("#assignmentEndTime").val()
        },
        success: function (data) {
            AssignmentList(courseId);
        }
    });
}

//点击 添加作业 按钮，显示 【添加】 button，隐藏 【修改】 button
var oShowAssignmentModal = document.getElementById("showAssignmentModal");
oShowAssignmentModal.onclick = function () {
    $("#changeAssignmentInfo").css("display","none");
    $("#addAssignment").css("display","block");
    $("#assignmentName").val("");
    $("#assignmentDescription").val("");
    $("#assignmentStartTime").val("");
    $("#assignmentEndTime").val("");
}

/* 活动 相关 的代码*/

// 获取 activity 列表
function ActivityList(assignmentId,courseId){

    $("#showModal").css("display","none");//隐藏创建课程的button
    $("#showAssignmentModal").css("display","none");//隐藏创建作业的button
    $("#showActivityModal").css("display","block");//显示创建活动的button

    $("#assignmentIdConnActivity").val(assignmentId);//将assignmentId存入一个hidden的input框

    $('#CourseTable').bootstrapTable('destroy');
    $('#CourseTable').bootstrapTable({
        //样式调整
        formatter: true,
        detailView: true,
        async: false,
        //后台获取数据
        url: 'getAllActivity',
        method: "post",
        contentType:"application/x-www-form-urlencoded; charset=UTF-8",
        queryParams: getActivityParams(assignmentId),
        dataType: "json",
        //id
        idField: 'activityId',
        columns: [{
            field: 'activityId',
            title: 'activityId',
            visible: false
        },{
            field: 'assignmentId',
            title: 'assignmentId',
            width: 100,
            visible: false
        },{
            field: 'activityName',
            title: '活动名',
            width: 100
        }, {
            field: 'type',
            title: '类型',
        },{
            field: 'description',
            title: '描述',
        },{
            field: 'startTime',
            title: 'startTime',
        },{
            field: 'endTime',
            title: 'endTime',
        },{
            field: 'operation',
            title: '操作',
            width: 200,
            formatter: function (value, row, index) {
                var jsonRow = JSON.stringify(row);
                return "<button class='btn btn-info btn-sm'" +
                    " data-toggle='modal' data-target='#myActivityModal' " +
                    "onclick='showModifyActivityInfoModal(" + jsonRow + ")'>编辑</button>" +
                    "&nbsp;&nbsp;<button class='btn btn-info btn-sm' data-toggle='modal' " +
                    "data-target='#myDeleteModal' onclick=showDeleteModal(" +
                    row.activityId + "," + assignmentId + ",'activity')>删除</button>";
            }
        }],
        detailFormatter: function (index, row) {
            //row.type=="对话"
            if (row.type == "对话") {
                html = '<div class="table-responsive">';
                $.ajax({
                    url: 'getStudentByCourseId',
                    async: false,
                    data: {
                        courseId: courseId
                    },
                    success: function (data) {
                        console.log(data.length);
                        if(data.length>0){
                            html += '<table class="table" id="childTable">';
                            html += '<tbody>';
                            $.each(data, function (index, user) {
                                html += '<tr>';
                                html += '<td style="display: none">' + user.id + '</td>';//userId固定
                                html += '<td>' + user.studentNumber + '</td>';
                                html += '<td>' + user.realname + '</td>';
                                html += '<td>';
                                html += '<button class="btn btn-info btn-sm" data-toggle="modal"'
                                html += 'data-target="#myDialogModal"  id="showDialogModal" ' +
                                    'onclick="getStuDialog(' + user.id + ',' + row.activityId + ')">' +
                                    '查看学生对话</button>';
                                html += '</td>';
                                html += '<td>';
                                html += '<button class="btn btn-info btn-sm" ' +
                                    'onclick="deleteDialog(' + user.id + ',' + row.activityId + ')">' +
                                    '删除学生对话</button>'
                                html += '</td>';
                                html += '</tr>'
                            });
                            html += '</tbody>';
                            html += '</table>';
                            html += '</div>'
                        }else {
                            html = '您还没有添加学生名单';
                        }
                    }
                });
                console.log(html);
                return html;
            }
            if(row.type=="讨论室"){
                var html = '<div class="table-responsive">';
                html += '<table class="table" id="childTable">';
                html += '<tbody>';
                html += '<tr>';
                html += '<td>小组1</td>';
                html += '<td>党雪婷，王彤</td>';
                html += '<td>';
                html += '<button class="btn btn-info btn-sm" ' +
                    'onclick=alert("功能还没有实现")>' +
                    '加入此小组讨论</button>';
                html += '</td>';
                html += '</tr>';
                html += '</tbody>';
                html +='</table>';
                html +='</div>'
                return html;
            }
        }
    });
}
//传递参数
getActivityParams = function (assignmentId) {
    var param = {
        assignmentId: assignmentId
    };
    return param;
}

//根据活动type跳转页面
jumpPage = function (type,activityId) {
    console.log("点击了跳转按钮: "+type);
    if(type=="讨论室"){
        window.location.href='./chatting.html?id='+user.id;
    }
    if(type=="对话"){
        window.location.href='./dialog.html?id='+user.id+"&activityId="+activityId;
    }
}
//获取学生的dialog内容
getStuDialog = function (userId,activityId) {
    $.ajax({
        type:'post',
        url: 'ReadDialog',
        data:{
            userId:userId,
            activityId:activityId
        },
        success: function (data) {
            $("#showDialog").css("display","block");
            $("#showDialog").html(data);
        }
    })
}

deleteDialog = function (userId,activityId) {
    $.ajax({
        type:'post',
        url: 'deleteDialogFile',
        data:{
            userId:userId,
            activityId:activityId
        },
        success: function (data) {
            alert(data);
        }
    })
}

//点击 添加活动 按钮，显示 【添加】 button，隐藏 【修改】 button
var oShowActivityModal = document.getElementById("showActivityModal");

oShowActivityModal.onclick = function () {
    $("#changeActivityInfo").css("display","none");
    $("#addActivity").css("display","block");
    $("#activityName").val("");
    $("#activityDescription").val(""),
    $("#activityStartTime").val("");
    $("#activityEndTime").val("");
}

//获取添加活动按钮
var oAddActivity = document.getElementById("addActivity");

//创建活动
oAddActivity.onclick = function () {

    console.log("创建的活动类型 type= "+$('#activityType option:selected').text());
    var assignmentId = $("#assignmentIdConnActivity").val();
    $("#myActivityModal").hide();
    $.ajax({
        url: 'createActivity',
        type: 'post',
        data: {
            assignmentId:assignmentId,
            activityName: $("#activityName").val(),
            type: $("#activityType option:selected").text(),//选中的文本
            description: $("#activityDescription").val(),
            startTime:$("#activityStartTime").val(),
            endTime:$("#activityEndTime").val()
        },
        success: function () {
            ActivityList(assignmentId);
        }
    });
}

//删除Activity
deleteActivity = function (event) {
    var activityId = event.data.elementId;
    var assignmentId = event.data.elementParam;
    $.ajax({
        url: 'deleteActivity',
        type: 'post',
        data: {
            activityId:activityId
        },
        success: function () {
            ActivityList(assignmentId);
        }
    })
}

//更新Activity信息
showModifyActivityInfoModal = function (jsonRow) {

    //获取activity的信息，并显示在输入框
    $("#changeActivityInfo").css("display","block");
    $("#addActivity").css("display","none");
    $("#activityId").val(jsonRow.activityId);
    $("#activityName").val(jsonRow.activityName);
    $("#activityType").val(jsonRow.type);
    $("#activityDescription").val(jsonRow.description);
    $("#activityStartTime").val(jsonRow.startTime);
    $("#activityEndTime").val(jsonRow.endTime);
}

// 修改活动信息
var oChangeActivityInfo = document.getElementById("changeActivityInfo");

oChangeActivityInfo.onclick = function () {
    $("#myActivityModal").hide();
    $.ajax({
        url: 'modifyActivity',
        type: 'post',
        data: {
            activityId: $("#activityId").val(),
            activityName: $("#activityName").val(),
            type: $('#activityType option:selected').text(),//选中的文本
            description: $("#activityDescription").val(),
            startTime: $("#activityStartTime").val(),
            endTime: $("#activityEndTime").val()
        },
        success: function () {
            ActivityList($("#assignmentIdConnActivity").val());
        }
    });
}

var jsonUser = getCookie("user");
console.log(jsonUser);
var user = JSON.parse(jsonUser);
if(user!=null){
    $("#userName").text(user.realname+"老师");
    //CourseList;
}
if(user==null){
    window.location.href='./index.html';
}
CourseList();